﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Web;
using System.Drawing.Imaging;

namespace GoodMan.Portal.WebLogic
{
    public class VerifyCodeImage
    {
        //随机颜色数组
        Color[] colors = { Color.Red, Color.Green, Color.Blue, Color.Black };

        //字体数组
        string[] fonts = { "Arial", "Georgia" };

        //波形滤镜效果
        private const double PI2 = 6.283185307179586476925286766559;
        /// <summary>
        /// 正弦曲线Wave扭曲图片
        /// </summary>
        /// <param name="srcBmp">图片路径</param>
        /// <param name="bXDir">如果扭曲则选择为True</param>
        /// <param name="nMultValue">波形的幅度倍数，越大扭曲的程度越高，一般为3</param>
        /// <param name="dPhase">波形的起始相位，取值区间[0-2*PI)</param>
        /// <returns></returns>
        private Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
        {
            Bitmap bitmap = new Bitmap(srcBmp.Width, srcBmp.Height);

            // 将位图背景填充为白色
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, bitmap.Width, bitmap.Height);
            graphics.Dispose();

            double dBaseAxisLen = bXDir ? (double)bitmap.Height : (double)bitmap.Width;

            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    double dx = 0;
                    dx = bXDir ? (PI2 * (double)j) / dBaseAxisLen : (PI2 * (double)i) / dBaseAxisLen;
                    dx += dPhase;
                    double dy = Math.Sin(dx);

                    // 取得当前点的颜色
                    int nOldX = 0, nOldY = 0;
                    nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
                    nOldY = bXDir ? j : j + (int)(dy * dMultValue);

                    Color color = srcBmp.GetPixel(i, j);
                    if (nOldX >= 0 && nOldX < bitmap.Width
                     && nOldY >= 0 && nOldY < bitmap.Height)
                    {
                        bitmap.SetPixel(nOldX, nOldY, color);
                    }
                }
            }
            return bitmap;
        }

        //生成校验码图片
        private Bitmap CreateImage(string code, double multValue)
        {
            int fSize = 25;
            int fWidth = fSize + 1;

            int imageWidth = (int)(code.Length * fWidth) + 4 + 1 * 2;
            int imageHeight = fSize * 2 + 1;

            Bitmap bitmap = new Bitmap(imageWidth, imageHeight, PixelFormat.Format16bppRgb555);

            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
            graphics.Clear(Color.White);

            Random rand = new Random();

            //给背景添加随机生成的燥点
            Pen pen = new Pen(Color.Blue, 1);
            int c = code.Length * 2;

            for (int i = 0; i < c; i++)
            {
                int x = rand.Next(bitmap.Width);
                int y = rand.Next(bitmap.Height);
                graphics.DrawLine(
                    pen,
                    new Point(rand.Next(bitmap.Width), rand.Next(bitmap.Height)),
                    new Point(rand.Next(bitmap.Width), rand.Next(bitmap.Height)));
                //graphics.DrawRectangle(pen, x, y, 1, 1);
            }

            int left = 0, top = 0, top1 = 1, top2 = 1;

            int n1 = (imageHeight - 40 - 1 * 2);
            int n2 = n1 / 4;
            top1 = n2;
            top2 = n2 * 2;

            Font font;
            Brush brush;

            int cindex, findex;

            // 随机字体和颜色的验证码字符
            for (int i = 0; i < code.Length; i++)
            {
                cindex = rand.Next(colors.Length - 1);
                findex = rand.Next(fonts.Length - 1);

                font = new Font(fonts[findex], fSize, FontStyle.Bold);
                brush = new SolidBrush(colors[cindex]);

                if (i % 2 == 1)
                {
                    top = top2;
                }
                else
                {
                    top = top1;
                }

                left = i * fWidth;

                graphics.DrawString(code.Substring(i, 1), font, brush, left, top);
            }

            // 画一个边框 边框颜色为Color.Gainsboro
            graphics.DrawRectangle(new Pen(Color.Gainsboro, 0), 0, 0, bitmap.Width - 1, bitmap.Height - 1);
            graphics.Dispose();

            // 产生波形
            bitmap = TwistImage(bitmap, true, multValue, 4);

            return bitmap;
        }

        /// <summary>
        /// 将创建好的图片输出到页面
        /// </summary>
        /// <param name="code">验证码</param>
        /// <param name="multValue">扭曲度(越大越扭曲)</param>
        /// <param name="httpContext">上下文</param>
        public void CreateImageOnPage(string code, double multValue, HttpContext httpContext)
        {
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            Bitmap bitmap = this.CreateImage(code, multValue);
            bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
            httpContext.Response.ClearContent();
            httpContext.Response.ContentType = "image/Png";
            httpContext.Response.BinaryWrite(memoryStream.GetBuffer());

            memoryStream.Close();
            memoryStream = null;
            bitmap.Dispose();
            bitmap = null;
        }
    }
}
